package com.oepm.web.controller;

import com.oepm.biz.EstabListBiz;
import com.oepm.common.entity.Meta;
import com.oepm.common.entity.Result;
import com.oepm.common.utils.JWTUtil;
import com.oepm.dao.entity.EstabListPo;
import com.oepm.dao.entity.QueryInfo;
import com.oepm.vo.DataVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/project")
public class EstabListController {
    @Autowired
    EstabListBiz estabListBiz;
    /**
     * 立项列表
     * @param queryInfo
     * @return
     */
    @GetMapping("/select")
    public Result selectAll(QueryInfo queryInfo,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String roleId = JWTUtil.getRoleId(token);
        String userId = JWTUtil.getUserId(token);
        DataVo dataVo;
        if(roleId.equals("4")){
            dataVo = estabListBiz.selectByTeaId(queryInfo,userId);
        }else{
            dataVo = estabListBiz.selectAll(queryInfo);
        }
        Result result = new Result(Meta.SUCCESS, dataVo);
        return result;
    }

    /**
     * 根据项目名搜索
     * @param pname
     * @return
     */
    @GetMapping("/selectId/{pname}")
    public Result selectByName(@PathVariable String pname){
        List<EstabListPo> list = estabListBiz.selectByName(pname);
        Result result;
        result = new Result(Meta.SUCCESS,list);
        return result;
    }

    /**
     * 立项/项目列表-改
     * @param po
     * @return
     */
    @PostMapping("/update")
    @RequiresRoles(value = {"4"})
    public Result updateStuInfo(@RequestBody EstabListPo po){
        int flag = estabListBiz.updateStuInfo(po);
        Result result;
        if (flag>0){
            result = new Result(Meta.SUCCESS);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }
}
